home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Muddweller 1.2 / source code / UMUDDweller.cp < prev    next >
Encoding:
Text File  |  1994-02-18  |  9.5 KB  |  387 lines  |  [TEXT/MPS ]

  1. /* UMUDDweller - Implementation of TMUDDwellerApp                             */
  2.  
  3. #include "UMUDDweller.h"
  4.  
  5.  
  6.         // • Toolbox
  7. #ifndef __ERRORS__
  8. #include "Errors.h"
  9. #endif
  10.  
  11. #ifndef __TOOLUTILS__
  12. #include "ToolUtils.h"
  13. #endif
  14.  
  15.  
  16.         // • MacApp
  17. #ifndef __UFailure__
  18. #include "UFailure.h"
  19. #endif
  20.  
  21.  
  22.         // • Implementation use
  23. #ifndef __GLOBALS__
  24. #include "Globals.h"
  25. #endif
  26.  
  27. #ifndef __MUDDOC__
  28. #include "MUDDoc.h"
  29. #endif
  30.  
  31. #ifndef __NOTRACE__
  32. #include "NoTrace.h"
  33. #endif
  34.  
  35. //------------------------------------------------------------------------------
  36.  
  37. const int kBusyTime = 10;
  38. const short kBusyCurID = 1000;
  39. const short kBusyCurMax = 7;
  40.  
  41. //------------------------------------------------------------------------------
  42.  
  43. #pragma segment MAApplicationRes
  44.  
  45. pascal void TMUDDwellerApp::BuildReserves (TDocument *preserve)
  46. {
  47.     while (gMemIsLow) {
  48.         if (gMemReserve && !gMemReserve2) {
  49.             gMemReserve2 = gMemReserve;
  50.             gMemReserve = NULL;
  51.         }
  52.         if (!gMemReserve2) gMemReserve2 = NewPermHandle (kRsrvSize);
  53.         if (gMemReserve2 && !gMemReserve)
  54.             gMemReserve = NewPermHandle (kRsrvSize);
  55.         if (IsHandlePurged (pMemReserve)) BuildAllReserves ();
  56.         gMemIsLow = (!gMemReserve || !gMemReserve2);
  57.         if (!DropSome (preserve)) break;
  58.     }
  59. }
  60.  
  61. //------------------------------------------------------------------------------
  62.  
  63. #pragma segment MATerminate
  64.  
  65. static Boolean canQuit;
  66.  
  67. pascal void QuitDoc (struct TDocument *aDocument, void * )
  68. {
  69.     TLogoutMacro *logout;
  70.     TMacro *mac;
  71.     
  72.     if (((TMUDDoc *) aDocument)->fClosing) {
  73.         mac = ((TMUDDoc *) aDocument)->fRunList;
  74.         while (mac->fNext) mac = mac->fNext;
  75.         ((TLogoutMacro *) mac)->fDoneCmd = cQuit;
  76.         canQuit = FALSE;
  77.     } else if (((TMUDDoc *) aDocument)->fConnected) {
  78.         logout = new TLogoutMacro;
  79.         FailNIL (logout);
  80.         logout->IMacro (aDocument, ((TMUDDoc *) aDocument)->fLogout);
  81.         logout->fDoneCmd = cQuit;
  82.         canQuit = FALSE;
  83.     }
  84. }
  85.  
  86. pascal void TMUDDwellerApp::Close (void)
  87. {
  88.     canQuit = TRUE;
  89.     ForAllDocumentsDo (QuitDoc, kDummyLink);
  90.     if (canQuit)
  91.         inherited::Close ();
  92.     else
  93.         gAppDone = FALSE;
  94. }
  95.  
  96. //------------------------------------------------------------------------------
  97.  
  98. #pragma segment MAApplicationRes
  99.  
  100. static Boolean gCMEvtHandled, gSwitchIn;
  101. static EventInfo *gCMEvtInfo;
  102. static ConnHandle gCMEvtConn;
  103.  
  104. pascal void CallCMEvent (struct TDocument *aDocument, void * )
  105. {
  106.     if (((TMUDDoc *) aDocument)->DoCMEvent (gCMEvtConn, gCMEvtInfo))
  107.         gCMEvtHandled = TRUE;
  108. }
  109.  
  110. pascal void CallCMResume (struct TDocument *aDocument, void * )
  111. {
  112.     ((TMUDDoc *) aDocument)->DoCMResume (gSwitchIn);
  113. }
  114.  
  115. pascal void TMUDDwellerApp::DispatchEvent (EventInfo *theEventInfo,
  116.         struct TCommand **commandToPerform)
  117. {
  118.     WindowPtr theWindow;
  119.     
  120.     theWindow = NULL;
  121.     switch (theEventInfo->thePEvent->what) {
  122.     case keyDown:
  123.     case autoKey:
  124.         theWindow = FrontWindow ();
  125.         break;
  126.     case mouseDown:
  127.         FindWindow (theEventInfo->thePEvent->where, &theWindow);
  128.         break;
  129.     case updateEvt:
  130.     case activateEvt:
  131.         theWindow = (WindowPtr) theEventInfo->thePEvent->message;
  132.         break;
  133.     }
  134.     gCMEvtHandled = FALSE;
  135.     if (theWindow != NULL) {
  136.         gCMEvtInfo = theEventInfo;
  137.         gCMEvtConn = (ConnHandle) GetWRefCon (theWindow);
  138.         ForAllDocumentsDo (CallCMEvent, kDummyLink);
  139.     }
  140.     if (gCMEvtHandled)
  141.         *commandToPerform = NULL;
  142.     else {
  143.         inherited::DispatchEvent (theEventInfo, commandToPerform);
  144.         if (theEventInfo->thePEvent->what == app4Evt) {
  145.             if (((theEventInfo->thePEvent->message >> 24) & 0xFF) ==
  146.                     suspendResumeMessage) {
  147.                 gSwitchIn = (Boolean) (theEventInfo->thePEvent->message & 1);
  148.                 ForAllDocumentsDo (CallCMResume, kDummyLink);
  149.             }
  150.         }
  151.     }
  152. }
  153.  
  154. //------------------------------------------------------------------------------
  155.  
  156. #pragma segment MAApplicationRes
  157.  
  158. pascal TCommand *TMUDDwellerApp::DoCommandKey (short ch, EventInfo *info)
  159. {
  160.     if (ch == '.') {
  161.         gStop = TRUE;
  162.         return NULL;
  163.     } else
  164.         return inherited::DoCommandKey (ch, info);
  165. }
  166.  
  167. //------------------------------------------------------------------------------
  168.  
  169. #pragma segment AOpen
  170.  
  171. pascal TDocument *TMUDDwellerApp::DoMakeDocument (CmdNumber )
  172. {
  173.     TMUDDoc *aMUDDoc;
  174.     Handle canPurge;
  175.  
  176.     if (TotalTempSize (FALSE, &canPurge) + 8 + GetHandleSize (pCodeReserve) >
  177.             pSzCodeReserve)
  178.         SetReserveSize (pSzCodeReserve, pSzMemReserve); /* forces rebuild */
  179.     BuildReserves (NULL);
  180.     if (gMemIsLow) Failure (memFullErr, 0);
  181.     aMUDDoc = new TMUDDoc;
  182.     FailNIL (aMUDDoc);
  183.     aMUDDoc->IMUDDoc ();
  184.     BuildReserves (NULL);
  185.     if (gMemIsLow) Failure (memFullErr, 0);
  186.     return aMUDDoc;
  187. }
  188.  
  189. //------------------------------------------------------------------------------
  190.  
  191. #pragma segment MAApplicationRes
  192.  
  193. pascal void TMUDDwellerApp::DoSetupMenus (void)
  194. {
  195.     if (gDisableMenus) {
  196.         TEvtHandler::DoSetupMenus ();
  197.         Enable (cAboutApp, TRUE);
  198.         Enable (cShowClipboard, TRUE);
  199.         SetMenuState (cShowClipboard, kIDBuzzString, bzShowClip, bzHideClip,
  200.             gClipWindow == GetActiveWindow ());
  201.         Enable (cClose, FALSE);
  202.     } else
  203.         inherited::DoSetupMenus ();
  204. }
  205.  
  206. //------------------------------------------------------------------------------
  207.  
  208. #pragma segment MAApplicationRes
  209.  
  210. pascal Boolean TMUDDwellerApp::DropSome (TDocument *preserve)
  211. {
  212.     long count;
  213.     Boolean dropped;
  214.     TMUDDoc *aMUDDoc;
  215.     TLogWindow *aLogWindow;
  216.     TLogView *aLogView;
  217.     TBigText *aBigText;
  218.     
  219.     dropped = FALSE;
  220.     count = GetHandleSize ((Handle) gMyDocList) / sizeof (Handle);
  221.     while (count-- > 0) {
  222.         aMUDDoc = (TMUDDoc *) ((**gMyDocList) [count]);
  223.         if (aMUDDoc != preserve) {
  224.             aLogWindow = aMUDDoc->fLogWindow;
  225.             if (!aLogWindow) break;
  226.             aLogView = aLogWindow->fLogView;
  227.             if (!aLogView) break;
  228.             aBigText = aLogView->fBT;
  229.             if (!aBigText) break;
  230.             dropped |= aBigText->DropSome ();
  231.         }
  232.     }
  233.     return dropped;
  234. }
  235.  
  236. //------------------------------------------------------------------------------
  237.  
  238. #pragma segment MAApplicationRes
  239.  
  240. static Boolean gAlertInhibit = FALSE;
  241.  
  242. pascal void TMUDDwellerApp::Idle (IdlePhase phase)
  243. {
  244.     if (IsHandlePurged (pMemReserve)) BuildAllReserves ();
  245.     if (gMemReserve && !gMemReserve2) {
  246.         gMemReserve2 = gMemReserve;
  247.         gMemReserve = NULL;
  248.     }
  249.     if (IsHandlePurged (pMemReserve) || !gMemReserve2 ||
  250.             (!gMemReserve && (phase == idleBegin))) {
  251.         if (IsHandlePurged (pMemReserve) || !gMemReserve2) {
  252.             DropSome (NULL);
  253.             gMemReserve2 = NewPermHandle (kRsrvSize);
  254.             BuildAllReserves (); /* might dellocate gMemReserve2 */
  255.         }
  256.         if (!gMemReserve && gMemReserve2)
  257.             gMemReserve = NewPermHandle (kRsrvSize);
  258.         if (gMemReserve && gMemReserve2) gMemIsLow = FALSE;
  259.     }
  260.     inherited::Idle (phase);
  261.     if (gPurgedHist && !gAlertInhibit) {
  262.         gAlertInhibit = TRUE;
  263.         StdAlert (phPurgedID);
  264.         gAlertInhibit = FALSE;
  265.         gPurgedHist = FALSE;
  266.     }
  267.     if ((gEventLevel <= 1) && (phase == idleContinue) && gDocList->IsEmpty ())
  268.         gStop = FALSE;
  269. }
  270.  
  271. //------------------------------------------------------------------------------
  272.  
  273. #pragma segment AInit
  274.  
  275. pascal void TMUDDwellerApp::IMUDDwellerApp (void)
  276. {
  277.     Str255 aName;
  278.     short aRef;
  279.     Handle aHandle;
  280.     FCBPBRec pb;
  281.     
  282.     gDefVRefNum = 0;
  283.     gDefDirID = 2;
  284.     gMacAppAlertFilter = (Ptr) (long) MacAppAlertFilter;
  285.     IApplication (kFileType);
  286.     GetAppParms (aName, &aRef, &aHandle);
  287.     pb.ioCompletion = NULL;
  288.     pb.ioNamePtr = aName;
  289.     pb.ioVRefNum = 0;
  290.     pb.ioRefNum = aRef;
  291.     pb.ioFCBIndx = 0;
  292.     FailOSErr (PBGetFCBInfo (&pb, FALSE));
  293.     gDefVRefNum = pb.ioFCBVRefNum;
  294.     gDefDirID = pb.ioFCBParID;
  295.     gDisableMenus = FALSE;
  296.     gStop = FALSE;
  297. }
  298.  
  299. //------------------------------------------------------------------------------
  300.  
  301. #pragma segment MASelCommand
  302.  
  303. static Boolean gCMMenuHandled;
  304. static short gCMMenu, gCMItem;
  305.  
  306. pascal void CallCMMenu (struct TDocument *aDocument, void * )
  307. {
  308.     if (((TMUDDoc *) aDocument)->DoCMMenu (gCMMenu, gCMItem))
  309.         gCMMenuHandled = TRUE;
  310. }
  311.  
  312. pascal struct TCommand *TMUDDwellerApp::MenuEvent (long menuItem)
  313. {
  314.     gCMMenu = (short) ((menuItem >> 16) & 0xFFFF);
  315.     gCMItem = (short) (menuItem & 0xFFFF);
  316.     gCMMenuHandled = FALSE;
  317.     ForAllDocumentsDo (CallCMMenu, kDummyLink);
  318.     if (gCMMenuHandled)
  319.         return NULL;
  320.     else
  321.         return inherited::MenuEvent (menuItem);
  322. }
  323.  
  324. //------------------------------------------------------------------------------
  325.  
  326. #pragma segment MAOpen
  327.  
  328. pascal void TMUDDwellerApp::SFGetParms (CmdNumber itsCmdNumber, short *dlgID,
  329.         Point *where, Ptr *fileFilter, Ptr *dlgHook, Ptr *filterProc,
  330.         TypeListHandle typeList)
  331. {
  332.     inherited::SFGetParms (itsCmdNumber, dlgID, where, fileFilter,dlgHook,
  333.         filterProc, typeList);
  334.     if (itsCmdNumber == cSendFile || itsCmdNumber == cUpload)
  335.         **typeList[0] = 'TEXT';
  336.     else if (itsCmdNumber == cPrefs)
  337.         **typeList[0] = 'APPL';
  338. }
  339.  
  340. //------------------------------------------------------------------------------
  341.  
  342. #pragma segment MAApplicationRes
  343.  
  344. static TWindow *actWindow;
  345. static TMUDDoc *actDoc;
  346.  
  347. pascal void FindFront (struct TDocument *aDocument, void * )
  348. {
  349.     if (((TMUDDoc *) aDocument)->fLogWindow == actWindow)
  350.         actDoc = (TMUDDoc *) aDocument;
  351. }
  352.  
  353. pascal Boolean TMUDDwellerApp::TrackCursor (void)
  354. {
  355.     long ticks;
  356.     Boolean result;
  357.     
  358.     if (gInBackground) return FALSE;
  359.     actWindow = WMgrToWindow (FrontWindow ());
  360.     actDoc = NULL;
  361.     ForAllDocumentsDo (FindFront, kDummyLink);
  362.     if (actDoc && actDoc->fRunning) {
  363.         gAlwaysTrackCursor = TRUE;
  364.         ticks = TickCount ();
  365.         if ((gLastBusyTick == 0) || (ticks - gLastBusyTick > kBusyTime)) {
  366.             gLastBusyTick = ticks;
  367.             if (qNeedsColorQD || gConfiguration.hasColorQD)
  368.                 RectRgn (gTempRgn, &(**GetMainDevice ()).gdRect);
  369.             else
  370.                 RectRgn (gTempRgn, &qd.screenBits.bounds);
  371.             UnionRgn (GetGrayRgn (), gTempRgn, gCursorRgn);
  372.             gBusyState = (gBusyState >= kBusyCurMax) ? 0 : gBusyState + 1;
  373.             SetCursor (*GetCursor (kBusyCurID + gBusyState));
  374.             ShowCursor ();
  375.             return TRUE;
  376.         } else
  377.             return FALSE;
  378.     } else {
  379.         result = inherited::TrackCursor ();
  380.         gAlwaysTrackCursor = FALSE;
  381.         gLastBusyTick = 0;
  382.         return result;
  383.     }
  384. }
  385.  
  386. //------------------------------------------------------------------------------
  387.